package cn.tedu.boot21.controller;

import cn.tedu.boot21.utils.DBUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Controller
public class UserController {
    @RequestMapping("/login")
    @ResponseBody
    public String login(String username,String password){
        try (Connection conn= DBUtils.getConn()){
            String sql= "select password from user where username=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            //替换
            ps.setString(1,username);
            //执行查询
            ResultSet rs=ps.executeQuery();
            //判断是否查询到了密码
            if(rs.next()){//满足条件代表查询到的,说明用户名存在
                //判断用户输入的和查询的正确的密码是否一致
                String pw=rs.getString(1);
                if (pw.equals(password)){
                    return "登入成功";
                }else {
                    return "密码错误";
                }
            }
            return "用户名不存在";
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "";
    }
}
